package com.jt.assistant.utils

import android.text.TextUtils
import com.jt.assistant.base.GlobalConfig.STORE_ROLE
import com.jt.assistant.base.GlobalConfig.TC_CARTON_PREFIX
import com.jt.assistant.base.GlobalConfig.TRANSFERSUPERVISOR
import com.jt.assistant.bean.ScanCarton
import com.jt.assistant.bean.ScanDataBean
import com.jt.assistant.bean.Waybill
import com.tencent.mmkv.MMKV

object ScanCartonCodeHelper {

     fun createReturnScanList(scanData: ScanDataBean) : ArrayList<ScanCarton>{
        val mScanCartonList = ArrayList<ScanCarton>()
        val scannedList: List<String> = scanData.scannedList
        val waybillList: List<Waybill> = scanData.waybillList

        for ((cartonNum1, _, channelOrderNo, _, _, waybillNo) in waybillList) {
            val cartonNum = cartonNum1.toFloat().toInt()
            for (i in 0 until cartonNum) {
                //    public ScanCarton(String cartonCode, String channelOrderNo, String waybillNo, int state, boolean hasUpload) {
                var cartonCode = returnComputeCartonByTransCode(waybillNo, i + 1)
                var scanned = scannedList.contains(cartonCode)
                if (!scanned) {
                    for (j in scannedList.indices) {
                        if (scannedList[j] == cartonCode) {
                            cartonCode = scannedList[j]
                            scanned = true
                        }
                    }
                }
                val scanCarton = ScanCarton(
                    cartonCode,
                    channelOrderNo,
                    waybillNo,
                    getReturnScanState(scanData),
                    scanned,
                    scanned,
                    scanData.dispatchCarId + ""
                )
                mScanCartonList.add(scanCarton)
            }
        }
        return mScanCartonList
    }


    private fun getReturnScanState(scanData: ScanDataBean?): Int {
        if (scanData == null) {
            return WaybillState.STATE_UNKOWN
        }
        var state: Int = scanData.state
        val truckType: Int = scanData.truckType
        if (state == WaybillState.STATE_RETURNS_TRANSPORTING) {
            if (truckType == 3) { //整车到无锡是整车卸车
                state = WaybillState.STATE_RETURNS_TRANSPORT_UNLOAD
            } else if (truckType == 4) { //整车到南通是待入仓
                state = WaybillState.STATE_RETURNS_WAITING_STORE
            }
        } else if (state == WaybillState.STATE_RETURNS_SHORT_TRANSPORTING) {
            state = WaybillState.STATE_RETURNS_WAITING_STORE
        } else if (state == WaybillState.STATE_RETURNS_GX_IN_TRANSIT) {
            state = WaybillState.STATE_RETURNS_GX_OUT_STORE
        } else if (state == WaybillState.STATE_RETURNS_WAITING_OUT_STORE) {
            state = WaybillState.STATE_RETURNS_OUT_OF_STOCK
        } else if (state == WaybillState.STATE_RETURNS_IN_DISTRIBUTION) {
            state = WaybillState.STATE_RETURNS_SIGN_UP
        }
        return state
    }



    fun createScanList(scanData: ScanDataBean): ArrayList<ScanCarton> {
        val mScanCartonList = ArrayList<ScanCarton>()
        val scannedList: List<String> = scanData.scannedList
        val waybillList: List<Waybill> = scanData.waybillList

        for ((cartonNum, channelNo, channelOrderNo, _, transCode, waybillNo) in waybillList) {
            if (channelNo == ChannelNo.CHANNEL_JD) {
                if (TextUtils.isEmpty(transCode)) { // jtCarton
                    for (i in 0 until cartonNum) {
                        //    public ScanCarton(String cartonCode, String channelOrderNo, String waybillNo, int state, boolean hasUpload) {
                        val cartonCode = String.format("%s%06d", waybillNo, i + 1)
                        val scanned = scannedList.contains(cartonCode)

                        val scanCarton = ScanCarton(
                            cartonCode,
                            channelOrderNo,
                            waybillNo,
                            getScanState(scanData),
                            scanned,
                            scanned,
                            scanData.dispatchCarId + ""
                        )
                        mScanCartonList.add(scanCarton)
                    }
                } else { //jdCarton
                    for (i in 0 until cartonNum) {
                        //    public ScanCarton(String cartonCode, String channelOrderNo, String waybillNo, int state, boolean hasUpload) {
                        var cartonCode: String = computeCartonByTransCode(transCode, i + 1)
                        var scanned = scannedList.contains(cartonCode)
                        if (!scanned) {
                            for (j in scannedList.indices) {
                                val s = scannedList[j].substring(
                                    0,
                                    11
                                ) + "00" + scannedList[j].substring(13)
                                if (s == cartonCode) {
                                    cartonCode = scannedList[j]
                                    scanned = true
                                }
                            }
                        }
                        val scanCarton = ScanCarton(
                            cartonCode,
                            channelOrderNo,
                            waybillNo,
                            getScanState(scanData),
                            scanned,
                            scanned,
                            scanData.dispatchCarId + ""
                        )
                        mScanCartonList.add(scanCarton)
                    }
                }
            } else if (channelNo == ChannelNo.CHANNEL_WP) {
                for (i in 0 until cartonNum) {
                    //    public ScanCarton(String cartonCode, String channelOrderNo, String waybillNo, int state, boolean hasUpload) {
                    val cartonCode = String.format("%s%06d", waybillNo, i + 1)
                    val scanned = scannedList.contains(cartonCode)
                    val scanCarton = ScanCarton(
                        cartonCode,
                        channelOrderNo,
                        waybillNo,
                        getScanState(scanData),
                        scanned,
                        scanned,
                        scanData.dispatchCarId + ""
                    )
                    mScanCartonList.add(scanCarton)
                }
            } else {
                for (i in 0 until cartonNum) {
                    val cartonCode = String.format("%s%06d", waybillNo, i + 1)
                    val scanned = scannedList.contains(cartonCode)
                    val scanCarton = ScanCarton(
                        cartonCode,
                        channelOrderNo,
                        waybillNo,
                        getScanState(scanData),
                        scanned,
                        scanned,
                        scanData.dispatchCarId + ""
                    )
                    mScanCartonList.add(scanCarton)
                }
            }
        }
        return mScanCartonList
    }


    //200701438888
    private fun returnComputeCartonByTransCode(transCode: String, index: Int): String {
        if (TextUtils.isEmpty(transCode)) {
            return ""
        }
        return if (transCode.startsWith(TC_CARTON_PREFIX)) {
            String.format("%s-%d-0-", transCode, index)
        } else {
            String.format("%s00%04d", transCode, index)
        }
    }


    private fun computeCartonByTransCode(transCode: String, index: Int): String {
        if (TextUtils.isEmpty(transCode)) {
            return ""
        }
        return if (transCode.startsWith(TC_CARTON_PREFIX)) {
            String.format("%s-%d-0-", transCode, index)
        } else {
            String.format("%s00%05d", transCode, index)
        }
    }

     fun getScanState(scanData: ScanDataBean?): Int {
        val userRoleList = MMKV.defaultMMKV().getString("ROLENAMELIST", "")

        if (scanData == null) {
            return WaybillState.STATE_UNKOWN
        }
        var state: Int = scanData.state
        if (state == WaybillState.STATE_WAITING_PICK_UP) {
            if (scanData.truckType == TruckType.PICK_UP_TO_TC) {
                state = WaybillState.STATE_STATE_PICKED_UP
                if (userRoleList!!.contains(TRANSFERSUPERVISOR)
                    || userRoleList.contains(STORE_ROLE)
                ) { //转运中心 203588829 203587646
                    state = WaybillState.STATE_WAITING_STORE //待入仓
                }
            } else if (scanData.truckType == TruckType.PICK_UP_TO_JD) {
                state = WaybillState.STATE_TRANSPORTING
            } else if (scanData.truckType == TruckType.PICK_UP_TO_FORWARDER) {
                state = WaybillState.STATE_SHORT_TRANSPORTING
            } else {
                state = WaybillState.STATE_STATE_PICKED_UP
            }
        } else if (state == WaybillState.STATE_WAITING_RECEIVE) {
            state = WaybillState.STATE_WAITING_STORE
        } else if (state == WaybillState.STATE_STATE_PICKED_UP) {
            state = WaybillState.STATE_WAITING_STORE
        } else if (state == WaybillState.STATE_HAS_IN_STORE) {
            state = WaybillState.STATE_WAITING_OUT_STORE
        } else if (state == WaybillState.STATE_SHORT_TRANSPORTING) {
            state = WaybillState.STATE_SHORT_TRANSPORT_UNLOAD
        }
        return state
    }

}